﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using System.IO;
using System.Xml;

using Cisoft.Booking.BLL;
using Cisoft.Booking.Model;

public partial class Business_Cisoft3G_OutboxManage_RemindFromExcel : WebUI.Page.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
        }

        //btnImport.Attributes.Add("onclick", "return confirm('您确定要导入数据吗？')");
    }

    #region 导入Excel
    protected void btnImport_Click(object sender, EventArgs e)
    {
        Label1.Text = "";

        if (FileUpload1.FileName != null && FileUpload1.FileName != "")
        {

            if (temCheckType(FileUpload1))
            {
                //上传文件返回服务器路径URL
                string strurl = "";
                strurl = doUpload(FileUpload1);
                if (strurl != "")
                {

                    if (doImport(strurl))
                    {
                        Label1.Text = "导入成功！";

                        //PageRight("导入成功！", "RemindManage.aspx", "返回 ", "");
                        //PageRight(ModalPopupExtender2, lblTS, btnConfirm2, "导入成功！");
                        //return;
                    }
                    else
                    {
                        Label1.Text = "导入失败！";

                        //PageError("导入失败！", "RemindManage.aspx", "返回重试或联系管理员 ", "");
                        //PageError(ModalPopupExtender2, lblTS, btnConfirm2, "导入失败！");
                        //return;
                    }
                }
                else
                {
                    Label1.Text = "导入文件过程中上传异常！";
                    //PageError(ModalPopupExtender2, lblTS, btnConfirm2, "<h4>导入文件过程中上传异常</h4>");
                    //return;
                }
            }
            else
            {
                Label1.Text = "导入文件类型不对！";
                //PageError(ModalPopupExtender2, lblTS, btnConfirm2, "<h4>导入文件类型不对！</h4>");
                //return;
            }
        }
        else
        {
            Label1.Text = "请选择导入的文件";
            //PageError(ModalPopupExtender2, lblTS, btnConfirm2, "<h4>请选择导入的文件</h4>");
            //return;
        }
    }
    #endregion

    #region 导入逻辑
    public bool doImport(string url)
    {
        Cisoft.Cisoft3G.BLL.RemindBO bllRemind = new Cisoft.Cisoft3G.BLL.RemindBO();
        Cisoft.Cisoft3G.Model.RemindMO modelRemind = new Cisoft.Cisoft3G.Model.RemindMO();
        bool result = false;
        Cisoft.Cisoft3G.BLL.ImportBO blldataset = new Cisoft.Cisoft3G.BLL.ImportBO();
        DataSet ds = new DataSet();
        #region  查询并导入
        ds = blldataset.GetDataSetBySqlExcel(url, "select * from [日程提醒信息$]");
        if (ds.Tables[0].Rows.Count > 0)
        {
            int all = 0;
            int ok = 0;
            int wrong = 0;

            all = ds.Tables[0].Rows.Count;
            //从第一行开始
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataSet dsishave = new DataSet();
                try
                {
                    if (ds.Tables[0].Rows[i]["提醒号码"].ToString() != "")
                    {
                        if (strSourceNumtest(ds.Tables[0].Rows[i]["提醒号码"].ToString()) && isnumber(ds.Tables[0].Rows[i]["提醒号码"].ToString()))
                        {
                            modelRemind.RM_Recive = ds.Tables[0].Rows[i]["提醒号码"].ToString();
                        }
                        else
                        {
                            show("第" + i + "行导入信息失败！" + "手机号码格式不正确");
                            wrong++;
                            continue;
                        }
                    }
                    else
                    {
                        show("第" + i + "行导入信息失败！" + "手机号码为空");
                        wrong++;
                        continue;
                    }

                    modelRemind.RM_CreateTime = DateTime.Now;
                    modelRemind.RM_EndTime = DateTime.Now;
                    if (ds.Tables[0].Rows[i]["截止时间"].ToString() == "")
                    {
                        modelRemind.RM_EndType = 0;
                    }
                    else
                    {
                        modelRemind.RM_EndTime = System.Convert.ToDateTime(ds.Tables[0].Rows[i]["截止时间"].ToString());
                        modelRemind.RM_EndType = 1;
                    }
                    modelRemind.RM_StandardType = 0;
                    modelRemind.RM_RateType = 0;
                    if (ds.Tables[0].Rows[i]["重复频率"].ToString() != "")
                    {
                        switch (ds.Tables[0].Rows[i]["重复频率"].ToString())
                        {
                            case "不重复":
                                modelRemind.RM_RateType = 0;
                                break;
                            case "每天":
                                modelRemind.RM_RateType = 1;
                                break;
                            case "每周":
                                modelRemind.RM_RateType = 2;
                                modelRemind.RM_StandardType = 1;
                                break;
                            case "每月":
                                modelRemind.RM_RateType = 3;
                                break;
                            case "每年":
                                modelRemind.RM_RateType = 4;
                                break;
                            default:
                                modelRemind.RM_RateType = 0;
                                break;
                        }
                    }
                    modelRemind.RM_RemindID = bllRemind.GetMaxId();
                    modelRemind.RM_Status = 0;

                    modelRemind.RM_StartTime = DateTime.Now;
                    modelRemind.RM_StandardTime = DateTime.Now;
                    modelRemind.RM_Title = DateTime.Now.ToString("yyyy-MM-dd hhmmss") + "提醒";

                    if (ds.Tables[0].Rows[i]["提醒时间"].ToString() != "")
                    {
                        modelRemind.RM_StartTime = System.Convert.ToDateTime(ds.Tables[0].Rows[i]["提醒时间"].ToString());
                        modelRemind.RM_StandardTime = System.Convert.ToDateTime(ds.Tables[0].Rows[i]["提醒时间"].ToString());
                        modelRemind.RM_Title = System.Convert.ToDateTime(ds.Tables[0].Rows[i]["提醒时间"].ToString()).ToString("yyyy-MM-dd hhmmss") + "提醒";
                    }

                    modelRemind.RM_StandardWeeks = ds.Tables[0].Rows[i]["星期几"].ToString();

                    if (modelRemind.RM_StandardWeeks == "")
                    {
                        switch (System.Convert.ToDateTime(modelRemind.RM_StandardTime).DayOfWeek)
                        {
                            case DayOfWeek.Sunday:
                                modelRemind.RM_StandardWeeks = "7";
                                break;
                            case DayOfWeek.Monday:
                                modelRemind.RM_StandardWeeks = "1";
                                break;
                            case DayOfWeek.Tuesday:
                                modelRemind.RM_StandardWeeks = "2";
                                break;
                            case DayOfWeek.Wednesday:
                                modelRemind.RM_StandardWeeks = "3";
                                break;
                            case DayOfWeek.Thursday:
                                modelRemind.RM_StandardWeeks = "4";
                                break;
                            case DayOfWeek.Friday:
                                modelRemind.RM_StandardWeeks = "5";
                                break;
                            case DayOfWeek.Saturday:
                                modelRemind.RM_StandardWeeks = "6";
                                break;
                        }
                    }

                    modelRemind.RM_SMContent = ds.Tables[0].Rows[i]["提醒内容"].ToString();










                    try
                    {
                        bllRemind.Add(modelRemind);
                        show("第" + i + "行导入信息成功！");
                        ok++;
                    }
                    catch (Exception ex)
                    {
                        show("第" + i + "行导入信息失败！" + ex.Message.ToString());
                        wrong++;
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    show("第" + i + "行" + "导入异常！" + ex.Message.ToString());
                    continue;
                }
            }

            show("一共需导入" + all + "条，成功" + ok + "条，失败" + wrong + "条！");
            result = true;
        }



        #endregion



        return result;
    }
    #endregion

    #region 导入检查
    public bool temCheckType(FileUpload inputfile)
    {
        bool result = false;
        string filename = inputfile.FileName;


        if (filename.LastIndexOf(".") > 0)
        {
            string exname = filename.Substring(filename.LastIndexOf(".") + 1, filename.Length - filename.LastIndexOf(".") - 1);

            exname = System.IO.Path.GetExtension(inputfile.FileName);

            if (exname == ".xls")
            {
                result = true;
                return result;
            }
            else
            {
                return result;
            }
        }

        return result;
    }
    #endregion

    #region 输出
    public void show(string str)
    {
        Response.Write(str + " <br/>");
    }
    #endregion

    #region  验证手机号码 public bool strSourceNumtest(string t)
    /// <summary>
    /// 验证手机号码
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public bool strSourceNumtest(string t)
    {

        bool re = false;
        if (t.Length == 11 && (t.Substring(0, 2) == "13" || t.Substring(0, 2) == "15" || t.Substring(0, 2) == "18"))
        {
            re = true;
        }


        return re;
    }
    #endregion

    #region 全为数字 public bool isnumber(string s)
    /// <summary>
    /// 全为数字 
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public bool isnumber(string s)
    {
        int Flag = 0;
        char[] str = s.ToCharArray();
        for (int i = 0; i < str.Length; i++)
        {
            if (Char.IsNumber(str[i]))
            {
                Flag++;
            }
            else
            {
                Flag = -1;
                break;
            }
        }
        if (Flag > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion

    #region 上传文件返回服务器URL
    public string doUpload(FileUpload FileUploadModel)
    {
        string strResult = "";
        if (FileUploadModel.PostedFile.FileName.Trim() != "")
        {
            String fileName = FileUploadModel.PostedFile.FileName.Substring(FileUploadModel.PostedFile.FileName.LastIndexOf("\\") + 1, FileUploadModel.PostedFile.FileName.Length - 1 - FileUploadModel.PostedFile.FileName.LastIndexOf("\\"));
            //取到当前时间的年、月、日、分、秒和毫秒的值,并使用字符串格式把它们组合成一个字符串

            String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()

            + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()

            + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()

            + DateTime.Now.Millisecond.ToString();

            //在时间字符串后面添加一个随机数和文件的后缀名

            String src = fileName.Substring(fileName.LastIndexOf(".") + 1, fileName.Length - fileName.LastIndexOf(".") - 1).ToLower();

            //后缀名限制
            //if (src.ToLower() == "rar" || src.ToLower() == "doc" || src.ToLower() == "xls")


            fileName = fileTime + GetRandomint(10000) + "." + src;

            ///上载文件到服务器硬盘

            try
            {

                FileUploadModel.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\uploads\\Remind\\" + fileName);
                strResult = Server.MapPath(Request.ApplicationPath) + "\\uploads\\Remind\\" + fileName;
            }
            catch (Exception ex)
            {
                return strResult;
            }


        }
        return strResult;

    }
    #endregion

    #region 默认产生一个小于10000的随机正整数
    private String GetRandomint(int nLimit)
    {
        int n = 10000;
        if (nLimit > 0)
        {
            n = nLimit;
        }
        Random random = new Random();
        //默认产生一个小于10000的随机正整数
        return (random.Next(n).ToString());
    }
    #endregion
}
